package com.view.component;

import com.DAL.DatabaseConnection;
import com.view.dialog.AddIncomeDialog;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

public class AnnouncementComponent extends Box {

    JFrame jFrame = null;
    private JTable table;

    private static Vector titles = new Vector();
    private static Vector<Vector> tableData = new Vector<>();
    public static DefaultTableModel tableModel;

    public AnnouncementComponent(JFrame jframe) {
        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图
        this.jFrame = jFrame;
        JPanel btnPanel = new JPanel();
        Color color = new Color(203, 220, 217);
        btnPanel.setBackground(color);
        btnPanel.setMaximumSize(new Dimension(2000, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton addBtn = new JButton("添加");
        JButton upBtn = new JButton("修改");
        JButton delBtn = new JButton("删除");

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //弹出一个对话框，让用户输入添加的信息
                new AddIncomeDialog();
            }
        });
        upBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //获取当前表格选中的id
                int selectedRow = table.getSelectedRow();//如果有选中的条目，返回条目的行号，如果没有选中，那么返回-1；
                if (selectedRow == -1) {
                    JOptionPane.showMessageDialog(jFrame, "请选中要修改的条目！", "提醒", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //获取该条目的第一个单元格，也就是id
                String id = tableModel.getValueAt(selectedRow, 0).toString();

                //弹出一个对话框，让用户输入修改的内容    TODO:新建面板
//                new UpdateIncomeDialog(id);
            }
        });
        delBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //获取当前表格选中的id
                int selectedRow = table.getSelectedRow();//如果有选中的条目，返回条目的行号，如果没有选中，那么返回-1；
                if (selectedRow == -1) {
                    JOptionPane.showMessageDialog(jFrame, "请选中要删除的条目！");
                    return;
                }
                //获取该条目的第一个单元格，也就是id
                String id = tableModel.getValueAt(selectedRow, 0).toString();
//                boolean flag = new Income_PayDao_Imp().deleteIncome(id);
//                if (flag) {
//                    JOptionPane.showMessageDialog(jFrame, "删除成功！");
//                    request();
//                    tableModel.fireTableDataChanged();
//                }

            }
        });

        btnPanel.add(addBtn);
        btnPanel.add(upBtn);
        btnPanel.add(delBtn);

        this.add(btnPanel);

        request();

        tableModel = new DefaultTableModel(tableData, titles);
        //把表格模组放到表格中，并重写表格中的isCellEditable方法
        table = new JTable(tableModel) {
            //重写，设置表格中的每一个单元格都不允许编辑
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        //设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        this.add(new JScrollPane(table));    //TODO:注意

    }


    public static void request() {
        //清空tableData的数据
        tableData.clear();
        titles.clear();
        //连接数据库
        Connection conn = DatabaseConnection.getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        //组装表格
        Object[] title = {"编号", "公告标题", "发布日期", "结束日期", "公告内容", "备注"};
        for (int i = 0; i < title.length; i++) {
            titles.add(title[i]);
        }
        String sql = "SELECT id,title,time,overtime,content,metro FROM announcement";
        try {
            statement = conn.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                Vector<Object> v = new Vector<>();

                int id = rs.getInt("id");
                String title2 = rs.getString("title");
                Date time = rs.getDate("time");
                Date overtime = rs.getDate("overtime");
                String content = rs.getString("content");
                String metro = rs.getString("metro");

                v.addElement(id);
                v.addElement(title2);
                v.addElement(time);
                v.addElement(overtime);
                v.addElement(content);
                v.addElement(metro);
                tableData.addElement(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseConnection.close(rs, statement, conn);
        }
    }

}
